<script setup lang="ts">
import { getHotRecommendAPI } from '@/services/hot'
import type { SubTypeItem } from '@/types/hot'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'

// 热门推荐页 标题和url  (注意：按理说标题可以使用后端返回的数据，而不是前端写死)
const hotMap = [
  { type: '1', title: '特惠推荐', url: '/hot/preference' },
  { type: '2', title: '爆款推荐', url: '/hot/inVogue' },
  { type: '3', title: '一站买全', url: '/hot/oneStop' },
  { type: '4', title: '新鲜好物', url: '/hot/new' },
]

//知识点【uniapp】： uniapp中接收路由传参推荐直接使用defineProps，而不是路由对象
const query = defineProps<{
  type: string
}>()
const currentHot = hotMap.find((item) => item.type === query.type)

// 设置页面标题
uni.setNavigationBarTitle({
  title: currentHot!.title,
})

// 推荐封面图
const bannerPicture = ref()
// 推荐选项对象  //知识点: 给对象添加一个属性,标志加载结束.TS中需要给对象类型添加该属性,使用&符号,注意加上大括号,和加减法运算逻辑一样
const subTypes = ref<(SubTypeItem & { finish?: boolean })[]>([])
// 选中的推荐选项索引
const selectedIndex = ref(0)

// 获取推荐列表数据
const getHotRecommendData = async () => {
  const res = await getHotRecommendAPI(currentHot!.url, {
    page: import.meta.env.DEV ? 33 : 1, //开发环境页面设置为33方便测试,生产环境为1.避免重复修改代码测试
    pageSize: 10,
  })
  bannerPicture.value = res.result.bannerPicture
  subTypes.value = res.result.subTypes
}

// 滚动触底
const onScrolltolower = async () => {
  const currentSubType = subTypes.value[selectedIndex.value]
  if (currentSubType.goodsItems.page >= currentSubType.goodsItems.pages) {
    //给对象添加一个属性,标志加载结束.TS中需要给对象类型添加该属性,使用&符号
    currentSubType.finish = true

    return uni.showToast({
      title: '没有更多数据了~',
      icon: 'none',
    })
  }
  currentSubType.goodsItems.page++

  const res = await getHotRecommendAPI(currentHot!.url, {
    subType: currentSubType.id,
    page: currentSubType.goodsItems.page,
    pageSize: currentSubType.goodsItems.pageSize,
  })

  // 追加数据
  const newSubTypes = res.result.subTypes[selectedIndex.value].goodsItems.items
  subTypes.value[selectedIndex.value].goodsItems.items.push(...newSubTypes)
}
onLoad(() => {
  getHotRecommendData()
})
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image :src="bannerPicture"> </image>
    </view>
    <!-- 推荐选项 -->
    <view class="tabs">
      <text
        v-for="(item, index) in subTypes"
        :key="item.id"
        :class="{ text: true, active: index === selectedIndex }"
        @tap="selectedIndex = index"
      >
        {{ item.title }}
      </text>
    </view>
    <!-- 知识点：tabs不同栏目展示不同页面的写法，如果后端返回的接口包含了所有type的tabs数据，可以像本项目这样写，先遍历完所有的数据，然后根据条件渲染对应的数据 -->
    <!-- 推荐列表 -->
    <scroll-view
      @scrolltolower="onScrolltolower"
      v-for="(item, index) in subTypes"
      :key="item.id"
      scroll-y
      class="scroll-view"
      v-show="selectedIndex === index"
    >
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goodsItem in item.goodsItems.items"
          :key="goodsItem.id"
          :url="`/pages/goods/goods?id=${goodsItem.id}`"
        >
          <image class="thumb" :src="goodsItem.picture"></image>
          <view class="name ellipsis">{{ goodsItem.desc }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ goodsItem.price }}</text>
          </view>
        </navigator>
      </view>
      <view class="loading-text">{{ item.finish ? '我是有底线的~' : '正在加载...' }}</view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}

.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;

  /* #ifdef APP-PLUS || H5 */
  uni-image {
    // 知识点[兼容性]:在H5和app端,uniapp的image标签实际渲染会多出一个uni-image的外层容器,为了让图片铺满,需要给这个容器也继承父亲的样式
    width: 100%;
    height: 100%;
  }
  /* #endif */
}

.scroll-view {
  flex: 1;
}

.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;

  .text {
    margin: 0 20rpx;
    position: relative;
  }

  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}

.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;

  .navigator {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  .thumb {
    width: 305rpx;
    height: 305rpx;
  }

  .name {
    height: 88rpx;
    font-size: 26rpx;
  }

  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }

  .symbol {
    font-size: 70%;
  }

  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>
